﻿Imports System.Net.NetworkInformation

Module Common
    Public DBacs As New clsOdbcDbIf
    Public g_szDBIpAddress As String
    Public g_szInitFileName As String
    Public g_fDBconnect As Boolean
    Public g_iDBconnResult As Integer
    Public Structure ServerDBacsTYPE
        Dim szDrive As String
        Dim szIPadr As String
        Dim szDsn As String
        Dim szDbn As String
        Dim szUid As String
        Dim szPas As String
        Dim iOver As Integer
    End Structure
    Public g_Server As ServerDBacsTYPE

    Public Function GetIPAddr() As String
        ' アダプタリストを取得する
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        GetIPAddr = String.Empty
        For Each adapter As NetworkInterface In adapters

            ' ネットワーク接続状態が UP のアダプタのみ表示 
            If (adapter.OperationalStatus = OperationalStatus.Up) Then

                Dim ip_prop As IPInterfaceProperties = adapter.GetIPProperties()

                ' ユニキャスト IPv4 アドレスの取得
                For Each addr As IPAddressInformation In ip_prop.UnicastAddresses()
                    If addr.Address.IsIPv6LinkLocal OrElse
                       addr.Address.IsIPv6Multicast OrElse
                       addr.Address.IsIPv6SiteLocal OrElse
                       addr.Address.IsIPv6Teredo Then
                        Continue For
                    End If

                    If addr.Address.ToString.Length > 16 Then
                        Continue For
                    End If

                    GetIPAddr = addr.Address.ToString
                    Exit Function
                Next

            End If
        Next

        If GetIPAddr.Length <= 0 Then
            Return ""
        End If
    End Function

End Module
